<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品详情价格</title>
</head>

<body>
    <div id="price"></div>
    <script>
        function getPrice(id) {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', 'product.json', true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    var product = JSON.parse(xhr.responseText);
                    document.getElementById('price').innerHTML = '<h1>' + product[id].name + '</h1><p>价格：' + product[id].price + '元</p>';
                } else if (xhr.status != 200 && xhr.readyState != 4) {
                    alert('网络错误，请检查网络连接');
                }
            };
            xhr.send();
        }
        getPrice(2); // 调用函数，传入商品id
    </script>
</body>

</html>